package com.example.demo.service.impl;

import com.example.demo.dataobject.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public boolean validateLogin(String telephone, String password) {

        User user = userRepository.findByTelephone(telephone);
        if (user == null) {
            return false;
        }
        if (!StringUtils.equals(password, user.getPassword())) {
            return false;
        }
        return true;
    }

    @Override
    public boolean register(String telephone, String password) {
        if(StringUtils.isEmpty(telephone)||StringUtils.isEmpty(password)){
            log.info("用户名或密码为空");
            return false;
        }
        User user = userRepository.findByTelephone(telephone);
        if(user!=null){
            log.info("用户已被注册");
            return false;
        }
        User addUser = new User(telephone, password);
        User save = userRepository.save(addUser);
        if(save!=null){
            return true;
        }
        return false;
    }
}
